home *** CD-ROM | disk | FTP | other *** search
/ Power Programmierung / Power-Programmierung (Tewi)(1994).iso / magazine / c_news / 15 / example.c < prev    next >
C/C++ Source or Header  |  1989-04-04  |  6KB  |  172 lines

  1. /* -- File:     example.c                                                   *
  2.    --                                                                       *
  3.    -- Author:   Anthony Lander                                              *
  4.    -- Date:     January 10, 1989.                                           *
  5.    --                                                                       *
  6.    -- Description:                                                          *
  7.    --           This is a small example program which uses the linked list  *
  8.    --           functions in LINE.C.  It creates a set of text lines, and   *
  9.    --           joins them together in a linked list.  It then demonstrates *
  10.    --           the sort of manipulations possible with the LINE.C library. *
  11.    --                                                                       */
  12.  
  13.  
  14.    
  15. #include <stdio.h>
  16. #include <malloc.h>
  17. #include <string.h>
  18. #include <stdlib.h>
  19. #include <conio.h>
  20.  
  21. #include "f_prot.h"
  22. #include "line.h"
  23.  
  24.  
  25.  
  26.  
  27.  
  28.  
  29.  
  30.  
  31.  
  32.  
  33.  
  34.  
  35.  
  36.  
  37.  
  38.  
  39.  
  40.  
  41.  
  42.  
  43.  
  44.  
  45.  
  46.  
  47. void main()
  48. {
  49.                             /* First, delcare some pointers of type _line   */
  50.     struct  _line *cur_line, *tmp_line;
  51.     char    txt[90];
  52.     int     x;
  53.  
  54.  
  55.     /* Start of by creating 20 lines, and adding them to the end of the list */
  56.  
  57.     for(x = 1; x <= 20; x++)  {
  58.         cur_line = get_new_line();      /* Get space to a ptr to line       */
  59.  
  60.         if(cur_line == NULL)  {     /* If no space, then stop allocating    */
  61.             break;                  /* having <20 lines makes no difference */
  62.         }
  63.  
  64.             /* Now create a line of text, and allocate space for it         */
  65.         sprintf(txt, "This is line number %d", x);
  66.         cur_line->string = malloc(strlen(txt)+1);
  67.  
  68.             /* place a copy in line->string, and save the length            */
  69.         strcpy(cur_line->string, txt);
  70.         cur_line->length = strlen(txt);
  71.  
  72.             /* -- To add cur_line to the end of the list, we first have to  *
  73.                -- figure out where the end of the list is!  That's done     *
  74.                -- with a call to get_last_line().                           *
  75.                --
  76.                -- No matter what get_last_line() returns (NULL or otherwise)*
  77.                -- it's a valid value, so we don't have to check to see that *
  78.                -- any of the list already exists                            */
  79.         tmp_line = get_last_line();
  80.  
  81.             /* Now add cur_line to the end of the list.                     */
  82.         insert_after_line(cur_line, tmp_line);
  83.      }
  84.  
  85.  
  86.     /* -------------------------------------------------------------------- */
  87.     /* OK, let's list all of the elements in the list (forwards, first)     */
  88.  
  89.     puts("\n\nAll the elements, listed forwards:\n");
  90.     cur_line = get_first_line();        /* get the first line               */
  91.  
  92.     /* -- The while() makes sure that we go until we're at the end of the   *
  93.        -- list                                                              */
  94.  
  95.     while(cur_line != NULL)  {
  96.         printf("%s, len=%d\n", cur_line->string, cur_line->length);
  97.  
  98.         /* -- This is an important line, it sets cur_line to point to the   *
  99.            -- element that follows it.  Pay close attention <grin>          */
  100.  
  101.         cur_line = (cur_line->next_line);
  102.     }
  103.  
  104.     printf("\n      Press any key to continue...");
  105.     getch();
  106.  
  107.  
  108.     /* -------------------------------------------------------------------- */
  109.     /* -- List everything backwards.  This is the same as the above while() *
  110.        -- except that we go                                                 *
  111.        --                       cur_line = (cur_line->prev_line);           *
  112.        --       instead of      cur_line = (cur_line->prev_line);           *
  113.        --                                                                   */
  114.  
  115.     puts("\n\nAll the elements, listed in reverse:\n");
  116.     cur_line = get_last_line();         /* get the LAST line this time      */
  117.  
  118.     while(cur_line != NULL)  {
  119.         printf("%s, len=%d\n", cur_line->string, cur_line->length);
  120.  
  121.         cur_line = (cur_line->prev_line);
  122.             /*                ^^^^                                          */
  123.     }
  124.  
  125.     printf("\n      Press any key to continue...");
  126.     getch();
  127.  
  128.     /* -------------------------------------------------------------------- */
  129.     /* -- Now let's delete the first line, and move the last line to be     *
  130.        -- the new first line (kid's stuff)                                  */
  131.  
  132.     cur_line = get_first_line();
  133.     delete_line(cur_line);
  134.  
  135.     cur_line = get_last_line();
  136.     tmp_line = get_first_line();
  137.  
  138.     move_before_line(cur_line, tmp_line);
  139.  
  140.     /* now here's the list again... */
  141.  
  142.     puts("\n\nFirst line deleted, last line moved to first position:\n");
  143.     cur_line = get_first_line();        /* get the first line               */
  144.  
  145.     while(cur_line != NULL)  {
  146.         printf("%s, len=%d\n", cur_line->string, cur_line->length);
  147.  
  148.         cur_line = (cur_line->next_line);
  149.     }
  150.  
  151.     printf("\n      Press any key to continue...");
  152.     getch();
  153.  
  154.     /* -------------------------------------------------------------------- */
  155.     /* -- ok, now let's delete all lines, and list again                    */
  156.  
  157.     delete_all_lines();
  158.  
  159.     puts("\n\nAll lines deleted:\n");
  160.     cur_line = get_first_line();        /* get the first line               */
  161.  
  162.     while(cur_line != NULL)  {
  163.         printf("%s, len=%d\n", cur_line->string, cur_line->length);
  164.  
  165.         cur_line = (cur_line->next_line);
  166.     }
  167.  
  168.     printf("\n      End of demonstration, Press any key to continue...");
  169.     getch();
  170. }
  171.  
  172.